Imports System.Linq

Namespace Aricie.DNN.Services.Url.FriendlyUrl



    Public Class GroupRewriteResult
        Private _RewriteEntity As Object
        Private _RewriteRegex As String = ""

        Private _ConsumedParameters As New Dictionary(Of UrlParam, Boolean)

        Public Sub New()
        End Sub

        Public Sub New(ByVal rewriteValue As String)
            Me.AddRewriteValue(rewriteValue)
        End Sub

        Public Sub New(ByVal rewriteValue As String, ByVal consumedParameters As Dictionary(Of UrlParam, Boolean))
            Me.New(rewriteValue)
            Me._ConsumedParameters = consumedParameters
        End Sub


        Public Sub Clear(clearConsumed As Boolean)
            Me.RewriteValues.Clear()
            If clearConsumed Then
                Me._ConsumedParameters.Clear()
            End If
        End Sub

        Public WriteOnly Property RewriteValue() As String
            'Get
            '    Return _RewriteValue
            'End Get
            Set(ByVal value As String)
                Me.Clear(False)
                Me.AddRewriteValue(value)
            End Set
        End Property

        Public Property RewriteValues As New List(Of KeyValuePair(Of String, String))

        Public Property HasValidEmptyContent() As Boolean

        Public Sub AddRewriteValue(value As String)
            Me.AddRewriteValue(String.Empty, value)
        End Sub

        Public Sub AddRewriteValue(key As String, value As String)
            Me.RewriteValues.Add(New KeyValuePair(Of String, String)(key, value))
        End Sub

        Public Property ConsumedParameters() As Dictionary(Of UrlParam, Boolean)
            Get
                Return _ConsumedParameters
            End Get
            Set(ByVal value As Dictionary(Of UrlParam, Boolean))
                _ConsumedParameters = value
            End Set
        End Property

        Public Property RewriteEntity() As Object
            Get
                Return _RewriteEntity
            End Get
            Set(ByVal value As Object)
                _RewriteEntity = value
            End Set
        End Property

        Public Property RewriteRegex() As String
            Get
                Return _RewriteRegex
            End Get
            Set(ByVal value As String)
                _RewriteRegex = value
            End Set
        End Property


        Public Sub ConsumeParameter(ByVal objUrlParam As UrlParam, ByVal used As Boolean)
            Me._ConsumedParameters(objUrlParam) = used
        End Sub

        '<Obsolete("use ConsumeParameter")> _
        'Public Sub AddParameter(ByVal objUrlParam As UrlParam, ByVal used As Boolean)
        '    Me.ConsumeParameter(objUrlParam, used)
        'End Sub


        ''' <summary>
        ''' Returns true if any rewritten value is different from the empty or null string
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function HasContent() As Boolean
            Return RewriteValues.Any(Function(kvp) Not String.IsNullOrEmpty(kvp.Value)) orelse HasValidEmptyContent
        End Function

        ''' <summary>
        ''' Returns content of the rewrite result, each key/value pair separated by the rewrittenValuesSeparator, 
        ''' and key and value separated by the keyAndValueSeparator
        ''' </summary>
        ''' <param name="rewrittenValuesSeparator"></param>
        ''' <param name="keyAndValueSeparator"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function RetrieveContent(rewrittenValuesSeparator As String, keyAndValueSeparator As String) As String
            Return String.Join(rewrittenValuesSeparator, RewriteValues.Select(Function(kvp) RetrieveRewrittenValue(kvp, keyAndValueSeparator)).ToArray())
        End Function

        Private Function RetrieveRewrittenValue(kvp As KeyValuePair(Of String, String), keyAndValueSeparator As String) As String
            Dim rewrittenValueBuilder As New StringBuilder
            'If we have a key, we set it up
            If Not String.IsNullOrEmpty(kvp.Key) Then
                rewrittenValueBuilder.Append(kvp.Key)
                rewrittenValueBuilder.Append(keyAndValueSeparator)
            End If
            rewrittenValueBuilder.Append(kvp.Value)
            Return rewrittenValueBuilder.ToString()
        End Function
    End Class
End Namespace
